package com.ldy.db;

public class Where<T> {

	private StringBuilder whereBuilder = new StringBuilder();

	/**
	 * 两个条件之间的拼接用and，表示“和”的关系
	 * 
	 * @return
	 */
	public Where<T> and() {
		whereBuilder.append(" and ");
		return this;
	}

	/**
	 * Add a BETWEEN clause so the column must be between the low and high parameters.
	 * 
	 * @param columnName
	 * @param low
	 * @param high
	 * @return
	 */
	public Where<T> between(String columnName, Object low, Object high) {
		if (low instanceof Integer && high instanceof Integer) {
			whereBuilder.append(columnName + " between " + low + " and " + high + " ");
		} else {
			whereBuilder.append(columnName + " between '" + low + "' and '" + high + "' ");
		}
		return this;
	}

	public Where<T> clear() {
		whereBuilder.replace(0, whereBuilder.length(), "");
		return this;
	}

	/**
	 * Add a '=' clause so the column must be equal to the value.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> eq(String columnName, Object value) {
		whereBuilder.append(columnName + " = " + value.toString());
		return this;
	}

	/**
	 * Add a '>=' clause so the column must be greater-than or equals-to the value.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> ge(String columnName, Object value) {
		whereBuilder.append(columnName + " >= " + value.toString());
		return this;
	}

	/**
	 * Add a '>' clause so the column must be greater-than the value.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> gt(String columnName, Object value) {
		whereBuilder.append(columnName + " > " + value.toString());
		return this;
	}

	public Where<T> idEq(int id) {
		return null;
	}

	/**
	 * Add a IN clause so the column must be equal-to one of the objects passed in.
	 * 
	 * @param columnName
	 * @param objects
	 * @return
	 */
	public Where<T> in(String columnName, Object... objects) {
		StringBuilder values = new StringBuilder();
		for (Object object : objects) {
			values.append(object.toString() + ",");
		}
		values.delete(values.length() - 1, values.length());
		whereBuilder.append(columnName + " in ( " + values.toString() + " ) ");
		return null;
	}

	/**
	 * Add a '<=' clause so the column must be less-than or equals-to the value.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> le(String columnName, Object value) {
		whereBuilder.append(columnName + " <= " + value.toString());
		return this;
	}

	/**
	 * Add a LIKE clause so the column must mach the value using '%' patterns.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> like(String columnName, Object value) {
		whereBuilder.append(columnName + " LINK '" + value.toString() +"' ");
		return this;
	}

/**
	 * Add a '<' clause so the column must be less-than the value.
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> lt(String columnName, Object value) {
		whereBuilder.append(columnName + " < " + value.toString());
		return this;
	}

	/**
	 * Add a '<>' clause so the column must be not-equal-to the value.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> ne(String columnName, Object value) {
		return null;
	}

	public Where<T> not() {
		return null;
	}

	public Where<T> notIn(String columnName, Object objects[]) {
		return null;
	}

	/**
	 * OR operation which takes the previous clause and the next clause and OR's them together.
	 * 
	 * @param columnName
	 * @param value
	 * @return
	 */
	public Where<T> or(String columnName, Object value) {
		whereBuilder.append(columnName + " or " + value.toString());
		return this;
	}

	public Where<T> query() {
		return null;
	}

	public T queryForFirst() {
		return null;
	}

	public String where() {
		return whereBuilder.toString();
	}
}
